﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace ActionSystem.Core.ActiveLogic
{
    public class BaseActiveLogicAbility
    {
        BaseStateMachine stateMachine;
        public BaseActiveLogicAbility(BaseStateMachine baseStateMachine)
        {
            stateMachine = baseStateMachine;
        }

        public virtual void ActiveLogicActionUpdate() 
        {
            //执行当前状态的逻辑
            var currentState = stateMachine.CurrentState;
            var currentTime = stateMachine.CurrentStateData;
            if (currentState == null)
            {
                return;
            }
            foreach (var action in currentState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IActiveLogicActionHandler)?.Update(action, currentTime.logicDeltaTime);
            }                                            
            //判断是否需要执行下一个状态
            var nextState = stateMachine.NextState;
            if (nextState == null)
            {
                return;
            }
            var nextTime = stateMachine.NextStateData;
            foreach (var action in nextState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IActiveLogicActionHandler)?.Update(action, nextTime.logicDeltaTime);
            }
            
                
        }
        public virtual void PostActiveLogicActionUpdate()
        {
            //执行当前状态的逻辑
            var currentState = stateMachine.CurrentState;
            var currentTime = stateMachine.CurrentStateData;
            if(currentState==null)
            {
                return;
            }
            foreach (var action in currentState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IActiveLogicActionHandler)?.PostUpdate(action, currentTime.logicDeltaTime);
            }

            //判断是否需要执行下一个状态
            var nextState = stateMachine.NextState;
            if (nextState == null)
            {
                return;
            }
            var nextTime = stateMachine.NextStateData;
            foreach (var action in nextState.GetActions())
            {
                if (action.IsExcuting)
                    (action.Handler as IActiveLogicActionHandler)?.PostUpdate(action, nextTime.logicDeltaTime);
            }

        }
    }
}


